﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace VideoCarousel.Control.VlC
{
    public class DoubleToTimeSpanStringConverter : IValueConverter
    {
        private static CultureInfo cultureInfo;
        static DoubleToTimeSpanStringConverter()
        {
            cultureInfo = new CultureInfo("fr-FR");
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                //double to timeSpanString
                double v = System.Convert.ToDouble(value);
                TimeSpan ts = TimeSpan.FromSeconds(v);
                return ts.ToString(@"hh\:mm\:ss", cultureInfo);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                string str = value.ToString();
                TimeSpan ts = new TimeSpan();
                if (TimeSpan.TryParseExact(str, @"hh\:mm\:ss", cultureInfo, out ts))
                {
                    return ts.TotalSeconds;
                }
                return 0d;
            }
            catch (Exception)
            {
                return 0;
            }

        }
    }
    public class TimeSpanToDouble : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is TimeSpan)
            {
                TimeSpan ts = (TimeSpan)value;
                return ts.TotalSeconds;
            }
            return 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double)
            {
                double val = (double)value;
                return TimeSpan.FromSeconds(val);

            }
            return TimeSpan.FromSeconds(0);
        }
    }
    public class TimeSpanToString : IValueConverter
    {
        private static CultureInfo cultureInfo;
        static TimeSpanToString()
        {
            cultureInfo = new CultureInfo("fr-FR");
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {

                TimeSpan ts = (TimeSpan)value;
                return ts.ToString(@"hh\:mm\:ss", cultureInfo);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                string str = value.ToString();
                TimeSpan ts = new TimeSpan();
                if (TimeSpan.TryParseExact(str, @"hh\:mm\:ss", cultureInfo, out ts))
                {
                    return ts.TotalSeconds;
                }
                return 0d;
            }
            catch (Exception)
            {
                return 0;
            }

        }
    }
}
